package www.wen.com.common.lib.view.popuview

import android.content.Context
import android.view.Gravity
import com.contrarywind.adapter.WheelAdapter
import com.contrarywind.view.WheelView
import com.lxj.xpopup.core.BottomPopupView
import com.lxj.xpopup.util.XPopupUtils
import www.wen.com.common.R
import www.wen.com.common.lib.helper.ResultUiCallBack

abstract class BaseSingleBottomSelectPopupView<T>(
        context: Context,
        var optionItems: List<T> = mutableListOf(),
        private val resultSelectedListenerCallBack: ResultUiCallBack<T>? = null,
) : BottomPopupView(context) {

    lateinit var mWheelView: WheelView

    var selectItem: T? = null

    override fun getMaxHeight(): Int = (XPopupUtils.getScreenHeight(context) * 0.45f).toInt()

    override fun onCreate() {
        super.onCreate()
        mWheelView = findViewById(R.id.wheelView)
        mWheelView.run {
            setTextSize(provideTextSize())
            setLineSpacingMultiplier(provideLineSpacingMultiplier())
            setCyclic(provideIsCyclic())
            setItemsVisibleCount(provideItemVisibleCount())
            setAlphaGradient(provideAlphaGradient())
            setDividerType(provideDividerType())
            setDividerColor(provideDividerColor())
            setTextColorCenter(provideTextColorCenter())
            setGravity(provideTextGravity())
            adapter = object : WheelAdapter<String> {

                override fun getItemsCount(): Int = optionItems.size

                override fun getItem(index: Int): String = provideItemString(optionItems[index])

                override fun indexOf(o: String): Int = provideIndexOf(o)

            }

            currentItem = provideSelectOptionIndex()
            selectItem = optionItems[currentItem]
            setOnItemSelectedListener { index ->
                selectItem = optionItems[index]
                resultSelectedListenerCallBack?.invoke(
                        this@BaseSingleBottomSelectPopupView,
                        optionItems[index],
                        this@BaseSingleBottomSelectPopupView)
            }
        }
    }

    abstract fun provideItemString(data: T): String

    abstract fun provideIndexOf(dataStr: String): Int

    open fun provideSelectOptionIndex(): Int = 0

    open fun provideAlphaGradient(): Boolean = false

    open fun provideDividerType(): WheelView.DividerType = WheelView.DividerType.FILL

    open fun provideTextSize(): Float = 15f

    open fun provideItemVisibleCount(): Int = 8

    open fun provideLineSpacingMultiplier(): Float = 2f

    open fun provideIsCyclic(): Boolean = false

    open fun provideDividerColor(): Int = R.color.colorECECEC

    open fun provideOutSideColor(): Int = R.color.color000000

    open fun provideBgColor(): Int = R.color.colorFFFFFF

    open fun provideTextColorCenter(): Int = R.color.color000000

    open fun provideTextGravity(): Int = Gravity.CENTER

}